package com.ys.controller.api.mall;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bo.vo.R;
import com.ys.entity.mall.TmGift;
import com.ys.entity.shiro.LoginUser;
import com.ys.enume.mall.TmGiftEnum;
import com.ys.service.mall.TmGiftService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.LayuiTableVoJA;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/api/inter/tmGift")
public class ApiInterTmGiftController {
	@Autowired
	private CommonApiService<TmGift> commonApiService;
	@Autowired
	private TmGiftService tmGiftService;

	@RequiresPermissions("tmGift:view")
	@RequestMapping("/search")
	@ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TmGift cond){
		cond.setOrderby("A.Id desc");
		return commonApiService.searchLay(cond);
	}

	/**
	 * 列出自己的礼品
	 * @param content
	 * @param page
	 * @return
	 */
	@RequestMapping("/listMyself")
	@ResponseBody
	public R listMyself(Integer page){
		
		LoginUser loginUser = ShiroUtils.getLoginUser();
		page = page == null ? 0 : page - 1;
		
		TmGift cond = new TmGift()
				.setUserId(loginUser.getUserId())
				.setState(TmGiftEnum.State.ABLE.value)
				.setPage(page)
				.setSize(8)
				.setOrderby("A.Id desc");
		List<ListDto.RowDto> rows = new ArrayList<ListDto.RowDto>();
		List<TmGift> listEntity = cond.queryList();
		for(TmGift entity : listEntity) {
			Long id = entity.getId();
			String code = entity.getCode();
			String name = entity.getGoodsName();
			String img = entity.getGoodsImg();
			rows.add(new ListDto.RowDto(id, name, code, img));
		}
		
		Integer totalNum = null;
		if (page == 0) {
			totalNum = cond.count();
		}
		ListDto dto = new ListDto(rows, totalNum);
		return R.ok(dto);
	}
	@Data
	@AllArgsConstructor
	public static class ListDto{
		private List<RowDto> rows; // 数据行
		private Integer totalNum; // 总条数
		
		@Data
		@AllArgsConstructor
		public static class RowDto {
			private Long id; // 字段ID
			private String name; // 字段名称
			private String code; // 编码
			private String img; // 封面图
		}
	}
	
	/**
	 * 使用礼品券
	 */
	@PostMapping("/use")
    @ResponseBody
	public R use(String bid, String code, String address){

		try {
			return tmGiftService.use(bid, code, address, ApiInterTmGiftUseController.MENU_ID);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}
	}
	
	/**
	 * 转赠礼品券
	 */
	@PostMapping("/send")
    @ResponseBody
	public R send(String bid, String code, String userIdTo){

		try {
			return tmGiftService.send(bid, code, userIdTo, ApiInterTmGiftGivenController.MENU_ID);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}
	}
}